package com.neu.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.neu.common.AutoLog;
import com.neu.common.CaptureConfig;
import com.neu.common.Result;
import com.neu.entity.Admin;
import com.neu.entity.Hire;
import com.neu.entity.Params;
import com.neu.exception.CustomException;
import com.neu.service.AdminService;
import com.neu.utils.SMSUtils;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private JavaMailSender sender;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping
    public Result findAll(){
        List<Admin> list = adminService.getAll();

        return Result.success(list);

    }

    @GetMapping("/search")
    public Result findBySearch(Params params) {
        PageInfo<Admin> info = adminService.findBySearch(params);
        return Result.success(info);
    }

    @GetMapping("/findPerson")
    public Result findBySearch(int id) {
        Admin admin = adminService.getPersonById(id);
        return Result.success(admin);
    }

    @GetMapping("/searchTutor")
    public Result findBySearchTutor(Params params) {
        PageInfo<Admin> info = adminService.findBySearchTutor(params);
        return Result.success(info);
    }

    @PostMapping
    public Result add(@RequestBody Admin admin){

        if(admin.getId() == null){
            adminService.add(admin);
        }else{
            adminService.update(admin);
        }

        return Result.success();
    }

    @PostMapping("/proposal")
    public Result proposal(@RequestBody Admin admin){
        adminService.proposal(admin);
        return Result.success();
    }

    @GetMapping("/getProposalUrl")
    public Result getProposalUrl(String name){
        String proposalUrl = adminService.getProposalUrl(name);
        return Result.success(proposalUrl);
    }

    @PostMapping("/proposalScore")
    public Result proposalScore(@RequestBody Admin admin){
        adminService.proposalScore(admin);
        return Result.success();
    }

    @GetMapping("/getProposal")
    public Result findProposal(String name){
        String proposal = adminService.findProposal(name);
        return Result.success(proposal);
    }

    @GetMapping("/getProposalScore")
    public Result findProposalScore(String name){
        String proposalScore = adminService.findProposalScore(name);
        return Result.success(proposalScore);
    }

    @PostMapping("/inspect")
    public Result inspect(@RequestBody Admin admin){
        adminService.inspect(admin);
        return Result.success();
    }

    @PostMapping("/inspectScore")
    public Result inspectScore(@RequestBody Admin admin){
        adminService.inspectScore(admin);
        return Result.success();
    }
//
    @GetMapping("/getInspect")
    public Result getInspect(String name){
        String proposal = adminService.getInspect(name);
        return Result.success(proposal);
    }
//
    @GetMapping("/getInspectScore")
    public Result getInspectScore(String name){
        String proposalScore = adminService.getInspectScore(name);
        return Result.success(proposalScore);
    }

    @PostMapping("/paper")
    public Result paper(@RequestBody Admin admin){
        adminService.paper(admin);
        return Result.success();
    }

    @PostMapping("/paperScore")
    public Result paperScore(@RequestBody Admin admin){
//        String name = (String) params.get("name");
//        Float paperScore = (Float) params.get("paperScore");
//        if(name == null || paperScore == null){
//            return Result.error("缺少参数");
//        }
        adminService.paperScore(admin);
        return Result.success();
    }
//    //
    @GetMapping("/getPaper")
    public Result getPaper(String name){
        String paper = adminService.getPaper(name);
        return Result.success(paper);
    }

    @GetMapping("/getScore1")
    public Result paperScore1(String name){

        String score1 = adminService.paperScore1(name);
        return Result.success(score1);
    }

    @GetMapping("/getScore2")
    public Result paperScore2(String name){

        String score1 = adminService.paperScore2(name);
        return Result.success(score1);
    }

    @GetMapping("/getScore3")
    public Result paperScore3(String name){

        String score1 = adminService.paperScore3(name);
        return Result.success(score1);
    }

    @GetMapping("/getScore4")
    public Result paperScore4(String name){

        String score1 = adminService.paperScore4(name);
        return Result.success(score1);
    }

    @GetMapping("/getScore5")
    public Result paperScore5(String name){

        String score1 = adminService.paperScore5(name);
        return Result.success(score1);
    }


//    //
//    @GetMapping("/getInspectScore")
//    public Result getInspectScore(String name){
//        String proposalScore = adminService.getInspectScore(name);
//        return Result.success(proposalScore);
//    }



    @PutMapping
    public Result update(@RequestBody Admin admin){
        adminService.updateTutor(admin);
        return Result.success();
    }

    @PutMapping("/betutor")
    public Result betutor(@RequestBody Admin admin){
        adminService.updateTutor(admin);
        return Result.success();
    }

    @PutMapping("/evaluate")
    public Result evaluate(@RequestBody Admin admin){
        adminService.evaluateTutor(admin);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        adminService.delete(id);
        return Result.success();
    }

    @PostMapping("/login")
    @AutoLog("登录系统")
    public Result login(@RequestBody Admin admin, @RequestParam String key, HttpServletRequest request){

        String captureInRedis = stringRedisTemplate.opsForValue().get(key);
//        //判断验证码对不对
//        if(!admin.getVerCode().toLowerCase().equals(CaptureConfig.CAPTURE_MAP.get(key))){
//            //如果不相等，说明验证不通过
//            CaptchaUtil.clear(request);
//
//            return  Result.error("验证码不正确");
//        }
        //判断验证码对不对
        if(!admin.getVerCode().toLowerCase().equals(captureInRedis)){
            //如果不相等，说明验证不通过
            CaptchaUtil.clear(request);

            return  Result.error("验证码不正确");
        }
        Admin loginUser = adminService.login(admin);
        //Admin loginUser = adminService.login(admin);
        //CaptureConfig.CAPTURE_MAP.remove(key);
        return Result.success(loginUser);
    }

//    @PostMapping("/login")
//    @AutoLog("登录系统")
//    public Result login(@RequestBody Admin admin){
//
//////        //判断验证码对不对
////        if(!admin.getVerCode().toLowerCase().equals(CaptureConfig.CAPTURE_MAP.get(key))){
////            //如果不相等，说明验证不通过
////            CaptchaUtil.clear(request);
////
////            return  Result.error("验证码不正确");
////        }
//
//
//        Admin loginUser = adminService.login(admin);
//        //Admin loginUser = adminService.login(admin);
//        //CaptureConfig.CAPTURE_MAP.remove(key);
//        return Result.success(loginUser);
//    }

//    @PostMapping("/login")
//    @AutoLog("Log in to the system")
//    public Result login(@RequestBody Admin admin, HttpServletRequest request){
//
//        Admin loginUser = adminService.login(admin);
//
//        return Result.success(loginUser);
//    }
    @PostMapping("/register")
    public Result register(@RequestBody Admin admin){
        adminService.add(admin);

        return Result.success();
    }

    @PutMapping("/reset")
    public Result resetPassword(@RequestBody Admin admin){
        adminService.updatePassword(admin);

        return Result.success();
    }

    @GetMapping("/getTutorInformation")
    public Result getTutorInformation(Integer id){
        Admin tutor = adminService.getTutorInformation(id);
        return Result.success(tutor);
    }

    @GetMapping("/echarts/bie")
    public Result bie(){
        //查询出所有助教
        List<Admin> list= adminService.findAll();
        Map<String,Long> collect = list.stream()
                .filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))
                .collect(Collectors.groupingBy(Admin::getTypeName,Collectors.counting()));
        //最后返回给前端的数据结构
        List<Map<String,Object>> mapList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(collect)){
            for(String key: collect.keySet()){
                Map<String,Object> map = new HashMap<>();
                map.put("name",key);
                map.put("value",collect.get(key));
                mapList.add(map);
            }
        }
        return Result.success(mapList);
    }


    @GetMapping("/echarts/bar")
    public Result bar(){

        List<Admin> list= adminService.findAll();
        Map<String,Long> collect = list.stream()
                .filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))
                .collect(Collectors.groupingBy(Admin::getTypeName,Collectors.counting()));
        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(collect)){
            for(String key: collect.keySet()){
                xAxis.add(key);
                yAxis.add(collect.get(key));
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("xAxis",xAxis);
        map.put("yAxis",yAxis);

        return Result.success(map);
    }

    @GetMapping("/sendMsg")
    public Result sendMsg(HttpServletRequest request){
        //获取手机号
        String phone = "15694278522";
//        String message = request.getParameter("message").toString();
        String message = request.getParameter("message");
        if (message == null) {
            return Result.error("The message content cannot be empty");
        }
        System.out.println("message : " + message);
        if(StringUtils.hasText(phone)){

            //调用阿里云提供的短信服务API完成发送短信
            SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",phone,message);


            return Result.success();
        }

        return Result.error("短信发送失败");
    }

    @GetMapping("/sendEmail")
    public Result sendEmail(HttpServletRequest request){
        //获取手机号
        String subject = request.getParameter("subject");
        String setText = request.getParameter("setText");
        String setTo = request.getParameter("setTo");
        String setFrom = request.getParameter("setFrom");

        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setSubject(subject);
        mail.setText(setText);
        mail.setTo(setTo);
        mail.setFrom(setFrom);
        sender.send(mail);
        System.out.println("邮箱发送完毕！");

        return Result.success();

    }



}
